package com.jy.wms.controller;


import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.SysFunction;
import com.jy.wms.service.SysFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * @ClassName: SysFunctionController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Wed Aug 30 18:14:19 GMT+08:00 2017
 */
/*
@Controller
@RequestMapping("/sysFunction")
@Api(value = "/sysFunction", description = "关于功能类型操作")
*/

@RestController
@RequestMapping("/sysFunction")
@Api(value = "/sysFunction", description = "关于功能类型操作")
public class SysFunctionController extends BaseController<SysFunctionService, SysFunction> {
    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    /**
     * 删除功能
     * @param ids
     * @return
     */

    @ApiOperation(value = "删除功能信息", response = JSONObject.class, notes = "删除功能信息")
    @PostMapping(value = "/delete")
    public @ResponseBody
    JSONObject deleteSysFunction(@RequestBody List<String> ids) {
        JSONObject json = new JSONObject();
        Integer flag = baseService.baseDeleteBathPrimaryKeys(ids);
        if (flag == 0) {
            json = SetStatus.setFunctionType(1, "error", "删除功能", "删除功能失败", "");
        } else {
            json = SetStatus.setFunctionType(1, "success", "删除功能", "删除功能成功", "");
        }
        return json;
    }

    /**
     * 增加功能
     *
     * @param entity
     * @return
     */
    @ApiOperation(value = "增加功能信息", response = JSONObject.class, notes = "增加功能信息")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addSysFunction(@RequestBody SysFunction entity) {
        JSONObject json = new JSONObject();
        entity.setId(UUIDUtils.uuid());
        /*entity.setCode("func_ "+entity.getCode());*/
        try {
            Integer a = baseService.getCountByCode(entity.getCode());
            if (a == 0) {

              baseService.baseInsert(entity);
                return SetStatus.setReturn(1, true, 1, "功能增加", "增加功能", JSONObject.fromObject(entity).toString());

            } else {
               return json = SetStatus.setFunctionType(1, "warning", "功能增加", "功能项编码重复", JSONObject.fromObject(entity).toString());
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "功能增加", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setReturn(1, true, 1, "功能增加", e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "功能增加", "增加功能", "");
        }
    }

  /*  @RequestMapping(value = "getSysFunction/{id}")
    public @ResponseBody SysFunction getSysFunctionById(@PathVariable("id") String id) {
        SysFunction entity=baseService.baseSelectByPrimaryKey(id,getSessionDB());
        return entity;
    }*/

    /**
     * 编辑功能
     *
     * @param entity
     * @return
     */
    @ApiOperation(value = "修改功能信息", response = JSONObject.class, notes = "修改功能信息")
    @PostMapping(value = "/edit")
    public @ResponseBody
    JSONObject editSysFunction(@RequestBody SysFunction entity) {
        JSONObject json = new JSONObject();
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setReturn(1, true, 1, "功能修改", "修改功能", JSONObject.fromObject(entity).toString());
        }  catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "功能修改", e.getMessage(), "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setReturn(1, true, 1, "功能修改", e.getMessage(), JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "功能修改", "修改功能", "");
        }
    }
}
